32
Beginner’s Guide to Code Algorithms
32
Here is the code that checks this in your program:
The variable WinOrLoss is 1 for a win.
Function WinOrLoss(ByVal Score As Double)
WinOrLoss = 0
Dim WinTag
WinTag = Array(0, 30, 238, 506, 627, 935, 1001, 1495, 7429)
For i = 1 To 8
If Score > 0 Then
If Score Mod WinTag(i) = 0 Then
WinOrLoss = 1
End If
End If
Next i
End Function
FIGURE 2.30 Win or loss.
:
:
2.6 HOW TO TEST DRIVE YOUR NEW CREATION
So far you have just run the program and expected it to work perfectly. In reality, that is
never the case. An average programmer runs and reruns a program at least a 100 times.
There is a tool in excel that allows you to do just this—run your program in a controlled
fashion. Without this tool, running a program is like driving a car without brakes. If it is
a perfect self-driving car, then you do not need to worry—it will drive itself.
But even a self-driving car needs human intervention for certain situations. So,
just as you need to learn how to use the brakes, steering wheel, and accelerator for a
car, you need to learn how to start, stop, examine, restart, and fix your code without
feeling intimidated.
This leads us to a discussion on the debugger.
This is a tool that allows you to test your code and examine the values of the variables
as they change through the program execution. You can pause anywhere and abandon
the test at any time.
Step 1 is to decide which program you are going to run and place the cursor any
where inside it. You would usually start with the very first program, that is typically
the “launcher” for the rest of the code. In our case of the Tic Tac Toe, this must be
the Subroutine called InitFormFirst. But that is only when you run it for the first
time. There are several execution points, as you have seen—double-clicking on a cell,
clicking on the “Play” button, etc.
One essential step in car driving is knowing your brakes—almost more important
than knowing how to start. Similarly, you can stop the program on any executable
line (an executable line is a line that has an action rather than a definition, such as a
Dim statement).